package ece.pdx.finalproject.roleplay;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.text.format.DateFormat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;


public class ToDoAdapter extends ArrayAdapter<ToDoList> 
{

	// List context
    private final Context context;
    // List values
    private final List<ToDoList> todoList;
	
	public ToDoAdapter(Context context, List<ToDoList> todoList) 
	{
		super(context, R.layout.list_layout, todoList);
		this.context = context;
		this.todoList = todoList;
		
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		 
        View rowView = inflater.inflate(R.layout.list_layout, parent, false);
        ListView lv=(ListView)parent;
        lv.setBackgroundColor(Color.rgb(0, 0, 0));
		lv.setDivider(new ColorDrawable(0xFFFFFFFF));
		lv.setDividerHeight(2); 
        TextView todoText = (TextView) rowView.findViewById(R.id.todoText);
        TextView roleText = (TextView) rowView.findViewById(R.id.roleText);
        TextView dateText = (TextView) rowView.findViewById(R.id.dateText);
        TextView dueText= (TextView) rowView.findViewById(R.id.dueText);
        todoText.setText(todoList.get(position).getText());
        roleText.setText(todoList.get(position).getRole());
 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        
        dateText.setText(todoList.get(position).getDate());
        
        Calendar calToday = new GregorianCalendar();
        
        Date dateToday = new Date();
        calToday.setTime(dateToday);
        
        int wk_yr_today = calToday.get(Calendar.WEEK_OF_YEAR);
        Date dueDate = new Date();
       
        try 
        {  
        	dueDate = dateFormat.parse(todoList.get(position).getDate());
        } 
        catch (ParseException e) 
        {  
            e.printStackTrace();  
        }
        
        Calendar calDueDate = new GregorianCalendar(); 
        calDueDate.setTime(dueDate);
        int wk_yr_duedate = calDueDate.get(Calendar.WEEK_OF_YEAR);
        
        if(wk_yr_today == wk_yr_duedate)
        {
        	dueText.setText("Due this week");
        }
        else
        if(wk_yr_duedate - wk_yr_today == 1)
        {
        	dueText.setText("Due next week");
        }
        else
      	if((wk_yr_duedate - wk_yr_today) > 1 && (wk_yr_duedate - wk_yr_today) <= 4)
      	{
      		dueText.setText("Due within a month");
      	}
      	else
      	{
      		dueText.setText("Lots more time left");
      	}
        
        return rowView;
	}
	
}
